package dev.html;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.swing.text.html.*;

public class HTMLParse extends HTMLEditorKit{
	private static final long serialVersionUID = 1L;
	public static int counter = 1;
	
	/**
	 * Call to obtain a HTMLEditorKit.Parser object.
	 * @return A new HTMLEditorKit.Parser object.
	 */
	public HTMLEditorKit.Parser getParser(){
		return super.getParser();
	}
	
	public static String getContentOfTag(String tag){
		if(tag== null || tag.trim().equalsIgnoreCase("")) return "";
		else return tag.substring(tag.indexOf(">")+1, tag.lastIndexOf("<"));
	}
	public static String getTagAndAttribute(String html, String tagName, String attrName, String attrValue){
		// TODO:
		return "";
	}
	public static String[] wizzFormat(String tag){
		String span1 = "<span class=\"username\">";
		String spane = "</span>";
		String div1 = "<div class=\"time\">";
		String dive = "</div>";
		String name = tag.substring(tag.indexOf(span1)+span1.length(), tag.indexOf(spane));
		String km = tag.substring(tag.indexOf(div1)+div1.length(), tag.indexOf(dive));
		String kma[] = (km!=null?km.split(" "):new String[0]);
		return new String[]{name, kma[0]};
	}
	
	public static void main(String[] args) {
		for(int i = 0; i< 500; i++){
			String htmlPage = HTMLPage.getPageAsString("http://concurs.libertatea.ro/wizz/participanti/" + i*10);
			MyParserCallback callb = new MyParserCallback(htmlPage);
			HTMLEditorKit.Parser parse = new HTMLParse().getParser();
			StringReader r = new StringReader(htmlPage);
			try {
				parse.parse(r, callb, true);
				List<String> tags = callb.getTagAndAttribute("div", "class", "box-user");
				Iterator<String> it = tags.iterator();
				while(it.hasNext()){
					try{
						String tag = it.next();
						String[] top = wizzFormat(tag);
						if(Integer.parseInt(top[1])>3000){
							System.out.println(top[0] + "\t" + top[1]);
						}
					}catch(Exception e){}
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

}
